package com.googlecode.cswish.struts.conversion;

import org.apache.struts2.components.Hidden;

import com.googlecode.cswish.model.PageElement;
import com.googlecode.cswish.model.PropertyInfo;
import com.googlecode.cswish.model.TextModel;
import com.googlecode.cswish.struts.Invoker;
import com.googlecode.cswish.struts.RestAction;

/**
 * Convert hidden object
 * 	 
 * @author jerry.feng Date: 2009-1-13
 */
public class HiddenElementConverter extends CommonElementConverter {

	@Override
	public PageElement convert(ConversionContext context) {
		PropertyInfo propertyInfo = context.getPropertyInfo();
		if (propertyInfo.isId()) {
			int level = context.getLevel();
			// action.model.childmodel.id
			// 0 --> 1 --> 2 --> 3
			boolean isChildModelId = (level >= 3);
			Invoker invoker = context.getInvoker();
			boolean isAdd = RestAction.isCreate(null, invoker.methodName);
			if (!isAdd && !isChildModelId || propertyInfo.isAutoId()) {
				PageElement element = super.convert(context);
				String type = Hidden.class.getName();
				element.setVisible(false);
				// Don't support text mode to avoid unnecessary trouble
				element.setTextModel(TextModel.FALSE);
				element.setType(type);
				return element;
			}
		}
		return null;
	}
}
